package com.example.springbooteasycode.utils.tablepdf.bad;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

import java.io.FileOutputStream;
import java.io.IOException;

public class DocumentCreator {
    private XWPFDocument document;

    public DocumentCreator() {
        document = new XWPFDocument();
    }

    public void createDocument(String filePath) {
        try (FileOutputStream out = new FileOutputStream(filePath)) {
            XWPFTable table = createTable();
            setCellHeight(table);
            document.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private XWPFTable createTable() {
        return document.createTable(3, 3); // 创建3行3列的表格
    }

    private void setCellHeight(XWPFTable table) {
        // 设置第一行的单元格高度
        XWPFTableRow row = table.getRow(0);
        for (int i = 0; i < row.getTableCells().size(); i++) {
            XWPFTableCell cell = row.getCell(i);
            cell.setText("Row 1 Column " + (i + 1));
            cell.getCTTc().getTcPr().addNewTcW().setW(4000); // 设置宽度
            cell.getCTTc().getTcPr().addNewVAlign().setVal(STVerticalJc.CENTER); // 垂直居中
            // 设置高度
//            cell.getCTTc().getTcPr().addNewTrHeight().setVal(600); // 单位为twips，600twips约为1/10英寸

        }
    }

    public static void main(String[] args) {
        DocumentCreator creator = new DocumentCreator();
        creator.createDocument("/Users/boykchen/Desktop/example.docx");
    }
}
